package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgProductCombinationDetailMapper;
import com.ygqh.baby.model.ProductPackQueryRequest;
import com.ygqh.baby.po.YgProductCombinationDetail;
import com.ygqh.baby.po.YgProductCombinationDetailExample;
import com.ygqh.baby.service.YgProductCombinationDetailService;
import com.ygqh.baby.utils.LocalArraysUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author admin
 */
@Service
public class YgProductCombinationDetailServiceImpl implements YgProductCombinationDetailService {

    @Autowired
    private YgProductCombinationDetailMapper ygProductCombinationDetailMapper;

    @Override
    public List<YgProductCombinationDetail> find() {
        YgProductCombinationDetailExample example = new YgProductCombinationDetailExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygProductCombinationDetailMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        YgProductCombinationDetailExample example = new YgProductCombinationDetailExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygProductCombinationDetailMapper.countByExample(example);
    }

    @Override
    public YgProductCombinationDetail findById(Long id) {
        return ygProductCombinationDetailMapper.selectByPrimaryKey(id);
    }


    @Override
    public int save(YgProductCombinationDetail ygProductCombinationDetail) {
        return ygProductCombinationDetailMapper.insertSelective(ygProductCombinationDetail);
    }

    @Override
    public int update(YgProductCombinationDetail ygProductCombinationDetail) {
        return ygProductCombinationDetailMapper.updateByPrimaryKeySelective(ygProductCombinationDetail);
    }

    @Override
    public ResultSet<YgProductCombinationDetail> search(QueryInfo queryInfo, String q) {
        List<YgProductCombinationDetail> list = ygProductCombinationDetailMapper.selectSuper(queryInfo, q);
        int count = ygProductCombinationDetailMapper.countSuper(q);
        ResultSet<YgProductCombinationDetail> resultSet = new ResultSet<YgProductCombinationDetail>(count, list);
        return resultSet;
    }

    @Override
    public int deleteSoft(Long[] ids) {
        Assert.notEmpty(ids);
        YgProductCombinationDetailExample example = new YgProductCombinationDetailExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        YgProductCombinationDetail record = new YgProductCombinationDetail();
        record.setStatus(DataStatus.Delete.name());

        return ygProductCombinationDetailMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int deleteSoft(Long id) {
        return this.deleteSoft(new Long[]{id});
    }

    @Override
    public int saveOrUpdate(Long productCombinationId, Long[] productIds, String updateBy, Date updateTime) {
        Assert.notNull(productCombinationId, "商品组合ID不能为空");
        Assert.notEmpty(productIds, "商品ID不能为空");
        List<Long> oldProductIdList = ygProductCombinationDetailMapper.selectProductIds(productCombinationId);
        List<Long> newProductIdList = LocalArraysUtil.convertToList(productIds);
        LocalArraysUtil.removeRepet(oldProductIdList, newProductIdList);
        if (CollectionUtils.isNotEmpty(newProductIdList)) {
            addBatch(productCombinationId, newProductIdList.toArray(new Long[0]), updateBy, updateTime);
        }
        if (CollectionUtils.isNotEmpty(oldProductIdList)) {
            ygProductCombinationDetailMapper.deleteBatch(productCombinationId, oldProductIdList, updateBy, updateTime);
        }
        return 1;
    }

    @Override
    public int addBatch(Long productCombinationId, Long[] longs, String updateBy, Date updateTime) {
        if (longs == null || longs.length == 0) {
            return 0;
        }
        List<YgProductCombinationDetail> list = new ArrayList<>(longs.length);
        for (Long pid : longs) {
            YgProductCombinationDetail detail = new YgProductCombinationDetail();
            detail.setProductCombinationId(productCombinationId);
            detail.setProductId(pid);
            detail.setCreateTime(updateTime);
            detail.setCreateBy(updateBy);
            detail.setUpdateTime(updateTime);
            detail.setUpdateBy(updateBy);
            detail.setStatus(DataStatus.Valid.name());
            list.add(detail);
        }
        return ygProductCombinationDetailMapper.addBatch(list);

    }

    @Override
    public int delete(Long productCombinationId, List<Long> productIds, String updateBy, Date updateTime) {
        return ygProductCombinationDetailMapper.deleteBatch(productCombinationId,productIds,updateBy,updateTime);
    }

}

